<?php
/*
 * @Author: zeyudada
 * @Date: 2022-01-14 19:12:26
 * @LastEditTime: 2022-02-21 20:26:15
 * @Description: 导入题目和答案，需要按照格式写好文件，题目里不允许有特殊字符
 * @Q Q: zeyunb@vip.qq.com(1776299529)
 * @E-mail: admin@zeyudada.cn
 * 
 * Copyright (c) 2022 by zeyudada, All Rights Reserved. 
 */

// 因为后台答案保存的不是ABCD，所以需要替换下
function myans(string $ans)
{
    switch ($ans) {
        case 'A':
            return 'options1';
            break;
        case 'B':
            return 'options2';
            break;
        case 'C':
            return 'options3';
            break;
        case 'D':
            return 'options4';
            break;

        default:
            return false;
            break;
    }
}

//题目文件
$file = file_get_contents('t.txt');
$ques = explode("\n\n", $file);

foreach ($ques as $value) {
    $que = explode("\n", $value);

    preg_match('/([0-9]{1,3})(.)([\x00-\xff]+)/', $que[0], $match);
    preg_match('/([A-D]{1,3})(.)([\x00-\xff]+/', $que[1], $o1match);
    preg_match('/([A-D]{1,3})(.)([\x00-\xff]+)/', $que[2], $o2match);
    preg_match('/([A-D]{1,3})(.)([\x00-\xff]+)/', $que[3], $o3match);
    preg_match('/([A-D]{1,3})(.)([\x00-\xff]+)/', $que[4], $o4match);

    $here['num'] = $match[1];
    $here['title'] = $match[3];
    $here['options1'] = $o1match[3];
    $here['options2'] = $o2match[3];
    $here['options3'] = $o3match[3];
    $here['options4'] = $o4match[3];

    $question[$here['num']] = $here;

    var_dump($here);
}

//答案文件
$da = file_get_contents('d.txt');
$das = explode("\n\n", $da);

foreach ($das as $value) {
    preg_match('/([0-9]{1,3}).([A-D])[\s]{1,5}解析：([\x00-\xff]{1,})/', $value, $pmatch);

    $answer['num'] = $pmatch[1];
    $answer['answer'] = $pmatch[2];
    $answer['parse'] = $pmatch[3];

    $question[$answer['num']]['answer'] = myans($pmatch[2]);
    $question[$answer['num']]['parse'] = $pmatch[3];
}

var_dump($question);

require('include/common.php');

# 导入进数据库
foreach ($question as $insert) {
    if ($DB->query("INSERT INTO `question`(`sort`, `title`, `options1`, `options2`, `options3`, `options4`, `answer`, `parse`) VALUES (1, '{$insert['title']}', '{$insert['options1']}', '{$insert['options2']}', '{$insert['options3']}', '{$insert['options4']}', '{$insert['answer']}', '{$insert['parse']}')") < 1) var_dump($insert);
}
